package io.renren.common.utils;



import io.renren.modules.api.entity.PsOrderRouteSectionEntity;
import io.renren.modules.psorder.entity.PsOrderEntity;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AutoNaviUtils {

	/**
	 *
	 * @param ids
	 */
	public static int DeleteDataById(String ids) {
		String paraForSign = "ids=" + ids + "&key=" + QwjGlobal.AUTONAVI_WEB_KEY + "&tableid=" + QwjGlobal.AUTONAVI_HTTP_TABLE_ID;
        String privateKey = QwjGlobal.AUTONAVI_WEB_PRIVATE_KEY;
        String sig = MD5Util.MD5Encode(paraForSign + privateKey, "utf-8");

        Map<String, String> paraMap = new HashMap<String, String>();
        paraMap.put("ids", ids);
        paraMap.put("key", QwjGlobal.AUTONAVI_WEB_KEY);
        paraMap.put("tableid", QwjGlobal.AUTONAVI_HTTP_TABLE_ID);
        paraMap.put("sig", sig);

        String sr = HttpUtil.sendPost(QwjGlobal.AUTONAVI_HTTP_DATA_DELETE_URL, paraMap);
        Map<String, Object> resultMap = QmJsonUtils.parseJSON2Map(sr);
        int status = Integer.valueOf(String.valueOf(resultMap.get("status"))).intValue();

        return status;
	  }
        
	/**
	 * 高德API计算行车距离
	 * @param startLng
	 * @param startLat
	 * @param endLng
	 * @param endLat
	 * @return
	 */
	public static String calculateCarDistance(String startLng,String startLat,String endLng,String endLat) {
		String startLoc = startLng+","+startLat;
    	String endLoc = endLng+","+endLat;
    	String paraForSign = "destination="+endLoc+"&key="+QwjGlobal.AUTONAVI_WEB_KEY
    			+"&origins="+startLoc;
  		String privateKey = QwjGlobal.AUTONAVI_WEB_PRIVATE_KEY;
  		String sig = MD5Util.MD5Encode(paraForSign+privateKey, "utf-8");
		
  		Map<String, String> paraMap = new HashMap<String, String>();
  		paraMap.put("destination",endLoc);
  		paraMap.put("key", QwjGlobal.AUTONAVI_WEB_KEY);
  		paraMap.put("origins", startLoc);
  		paraMap.put("sig", sig);
  		
  		String sr = HttpUtil.sendPost(QwjGlobal.AUTONAVI_HTTP_CAR_DISTANCE_URL,paraMap);
  		Map<String, Object> resultMap = QmJsonUtils.parseJSON2Map(sr);
  		System.out.println(resultMap);
  		@SuppressWarnings("unchecked")
		ArrayList<Map<String, Object>> results = (ArrayList<Map<String, Object>>) resultMap.get("results");
		Map<String, Object> result = results.get(0);
		return (String)result.get("distance");
	}

	/**
	 *  根据多目的地路段对象集合,通过高德API计算总行驶距离
	 * @param psOrderRouteSectionList 多目的地路段对象集合
	 * @return
	 */
	public static String evaluateOrderDistance(List<PsOrderRouteSectionEntity> psOrderRouteSectionList) {
		BigDecimal ditance = new BigDecimal("0");
		for (PsOrderRouteSectionEntity psOrderRouteSection:psOrderRouteSectionList) {
			String ditanceValueTemp = AutoNaviUtils.calculateCarDistance(psOrderRouteSection.getStartLng(),psOrderRouteSection.getStartLat()
					,psOrderRouteSection.getEndLng(),psOrderRouteSection.getEndLat());
			ditance = ditance.add(new BigDecimal(ditanceValueTemp));
		}

		return ditance.stripTrailingZeros().toPlainString();
	}

	/**
	 * 8种车型方案
	 * @param psOrder 配送订单实体
	 */
	public static void evaluateOrderMoneyByType(PsOrderEntity psOrder,String ditanceValue) {
		double douleKiloMetres = (DataTypeUtils.stringToDouble(ditanceValue))/1000;
		double orderMoney = 0.0;
		double passKiloMetres = 0.0; //超里程数(公里),默认为0表示默认5公里内
		if ( douleKiloMetres - 5.0 > 0 ) {
			passKiloMetres = douleKiloMetres - 5.0;
		}
		if ("1".equals(psOrder.getVehicleType())) {  //小面包车V2
			psOrder.setStartingPrice(new BigDecimal("30"));
			psOrder.setMilegeUnitPrice(new BigDecimal(3));
			orderMoney = passKiloMetres*3 + psOrder.getStartingPrice().doubleValue();
		} else if ("2".equals(psOrder.getVehicleType())) { //中面包D09（金杯型）
			psOrder.setStartingPrice(new BigDecimal("60"));
			psOrder.setMilegeUnitPrice(new BigDecimal(5));
			orderMoney = passKiloMetres*5 + psOrder.getStartingPrice().doubleValue();
		} else if ("3".equals(psOrder.getVehicleType()) || "4".equals(psOrder.getVehicleType()) || "6".equals(psOrder.getVehicleType())) { //大面包D11（凌特型）或 厢货KM 或厢货WK
			psOrder.setStartingPrice(new BigDecimal("90"));
			psOrder.setMilegeUnitPrice(new BigDecimal(6));
			orderMoney = passKiloMetres*6 + psOrder.getStartingPrice().doubleValue();
		}  else if ("5".equals(psOrder.getVehicleType())) { //千万家小厢货KM
			psOrder.setStartingPrice(new BigDecimal("30"));
			psOrder.setMilegeUnitPrice(new BigDecimal(4));
			orderMoney = passKiloMetres*4 + psOrder.getStartingPrice().doubleValue();
		}else if ("7".equals(psOrder.getVehicleType()) || "8".equals(psOrder.getVehicleType())) { //千万家厢货冷链LKM 或者 千万家厢货冷链LWK
			psOrder.setStartingPrice(new BigDecimal("160"));
			psOrder.setMilegeUnitPrice(new BigDecimal(8));
			orderMoney = passKiloMetres*8 + psOrder.getStartingPrice().doubleValue();
		}
		psOrder.setOrderMoney(orderMoney);
	}

	/**
	 *  6种车型方案
	 * @param psOrder 配送订单实体
	 */
	public static void evaluateOrderMoneyByType2(PsOrderEntity psOrder, String ditanceValue) {
		double douleKiloMetres = (DataTypeUtils.stringToDouble(ditanceValue))/1000;
		double orderMoney = 0.0;
		double passKiloMetres = 0.0; //超里程数(公里),默认为0表示默认5公里内
		if ( douleKiloMetres - 5.0 > 0 ) {
			passKiloMetres = douleKiloMetres - 5.0;
		}
		DecimalFormat df = new DecimalFormat("#.00");
		psOrder.setOverMilege(new BigDecimal(df.format(passKiloMetres)));
		if ("1".equals(psOrder.getVehicleType())) {                 //同城小件
			psOrder.setStartingPrice(new BigDecimal("20"));
			psOrder.setMilegeUnitPrice(new BigDecimal(3));
			orderMoney = passKiloMetres*3 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*3)));
		} else if ("2".equals(psOrder.getVehicleType())) {          //小面包
			psOrder.setStartingPrice(new BigDecimal("30"));
			psOrder.setMilegeUnitPrice(new BigDecimal(3));
			orderMoney = passKiloMetres*3 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*3)));
		} else if ("3".equals(psOrder.getVehicleType())) {          //中面包
			psOrder.setStartingPrice(new BigDecimal("60"));
			psOrder.setMilegeUnitPrice(new BigDecimal(5));
			orderMoney = passKiloMetres*5 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*5)));
		}  else if ("4".equals(psOrder.getVehicleType())) {         //依维柯
			psOrder.setStartingPrice(new BigDecimal("90"));
			psOrder.setMilegeUnitPrice(new BigDecimal(6));
			orderMoney = passKiloMetres*6 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*6)));
		}else if ("5".equals(psOrder.getVehicleType())) {           //厢货
			psOrder.setStartingPrice(new BigDecimal("90"));
			psOrder.setMilegeUnitPrice(new BigDecimal(6));
			orderMoney = passKiloMetres*6 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*6)));
		}else if ("6".equals(psOrder.getVehicleType())) {           //冷链
			psOrder.setStartingPrice(new BigDecimal("160"));
			psOrder.setMilegeUnitPrice(new BigDecimal(8));
			orderMoney = passKiloMetres*8 + psOrder.getStartingPrice().doubleValue();
			psOrder.setOverMilegePrice(new BigDecimal(df.format(passKiloMetres*8)));
		}

		psOrder.setOrderMoney(Double.parseDouble(df.format(orderMoney)));
		psOrder.setTotalPrice(new BigDecimal(df.format(orderMoney)));

	}

}
